// 导入依赖的模块
yepjs.require("jquery");
yepjs.require("YBA.Common");
yepjs.require("YBA.Plugin.plugins");
yepjs.require("YBA.Data.Admin");


// 终端管理 - 新增终端
yepjs.define("add", {

    NS: "YBA.Logic.Admin",

    field: function() {

        var Biz = {
            // 初始化事件
            init: function() {
                for (var i in Biz.event) {
                    Biz.event[i]();
                }
            },
            //  公共变量
            vars: {
                // 模板列表
                tpl: (function(){
                    var tpl = {};
                    $("script[type='text/ysmart']").each(function(){
                        var t = $(this);
                        tpl[t.attr("data-name")] = t.html();
                    });
                    return tpl;
                })(),
                // 搜索字段
                searchVar: {
                	startPos: 0,
                    pageSize: 0
                },
            },
            // 页面元素
            el: {
                body: $("body"),
                jcheck:$(".j-check"),
                jform:$(".form-page"),
                jaddBtn:$("#j-addBtn"),
                jresetBtn:$("#j-resetBtn"),
                jdate:$(".j-date"),
                jthisUser:$(".j-thisUser"),
                jcaId:$("#j-caId"),
                jwhatCa:$(".j-whatCa"),
                jwhatEncrypt:$(".j-whatEncrypt"),
                jwhatFile:$(".j-whatFile"),
                jisEncryptSrc:$("#j-isEncryptSrc"),
                jradio:$(".j-radio"),
                jwatchSize:$(".j-watchSize"),
                jalertUserBtn:$("#j-alertUserBtn"),
                jautUserBtn:$("#j-autUserBtn"),
                jalertUserList:$("#j-alertUserList"),
                jautUserList:$("#j-autUserList"),
                jalertsId:$("#j-alertsId"),
                jautsId:$("#j-autsId"),
                jspaceSize:$("#j-spaceSize"),
                jpackage:$("#j-package")


            },
            // 功能函数集合
            fn: {
                // 表单验证
                formCheck: function(form,name) {
                    // 显示错误信息
                    var showError = function(obj, msg) {
                        var msgDom = obj.siblings('.message');
                        var msgTxt = msgDom.children('i')
                        msgDom.addClass("msg-error").removeClass('msg-tip');
                        msgTxt.html(msg);
                        msgDom.show();
                    };

                    // 隐藏错误信息
                    var hideError = function(obj, msg) {
                        var msgDom = obj.siblings('.message');
                        var msgTxt = msgDom.children('i')
                        msgDom.hide();
                        msgDom.addClass("msg-tip").removeClass('msg-error');
                        msgTxt.html(msg);
                    };

                    // 数据验证对象
                    var dataCheck = {
                        password:{
                            obj: form.find("[name='password']"),
                            fn: function() {
                                var val = this.obj.val();
                                if (YBA.Common.check.isNull(val)) {
                                    showError(this.obj,"请输入密码");
                                    return false;
                                }else if(!YBA.Common.check.isPwd(val)){
                                	showError(this.obj,"至少六位的英文数字组合");
                                    return false;
                                }else if(YBA.Common.check.isHowLong(val,50)){
                                	showError(this.obj,"密码过长");
                                    return false;
                                } else {
                                    hideError(this.obj,"请输入密码");
                                    return true;
                                }
                            }
                        },
                        confirmPassword: {
                            obj: form.find("[name='confirmPassword']"),
                            fn: function() {
                                var val = this.obj.val();
                                if (YBA.Common.check.isNull(val)) {
                                    showError(this.obj,"请再次输入密码");
                                    return false;
                                } else if(val != dataCheck["password"].obj.val()){
                                    showError(this.obj,"两次输入的密码不一致");
                                    return false;
                                }else {
                                    hideError(this.obj,"请再次输入密码");
                                    return true;
                                }
                            }
                        },
                        companyName: {
                            obj: form.find("[name='companyName']"),
                            fn: function() {
                                var val = this.obj.val();
                                if (YBA.Common.check.isNull(val)) {
                                    showError(this.obj,"请输入公司名称");
                                    return false;
                                }else if(YBA.Common.check.isHowLong(val,100)){
                                	showError(this.obj,"公司名称过长");
                                    return false;
                                } else {
                                    hideError(this.obj,"请输入公司名称");
                                    return true;
                                }
                            }
                        },
                        /*spaceSize: {
                            obj: form.find("[name='spaceSize']"),
                            fn: function() {
                                var val = this.obj.val();
                                if (YBA.Common.check.isNull(val)) {
                                    showError(this.obj,"请输入空间大小");
                                    return false;
                                } else if(YBA.Common.check.isHowLong(val,45)){
                                	showError(this.obj,"分配空间过大");
                                    return false;
                                } else if(!YBA.Common.check.isNum(val,{"min":1,"decimals":0})){
                                	showError(this.obj,"请输入正整数");
                                    return false;
                                } else {
                                    hideError(this.obj,"请输入空间大小");
                                    return true;
                                }
                            }
                        },*/
                        maxCopys: {
                            obj: form.find("[name='maxCopys']"),
                            fn: function() {
                                var val = this.obj.val();
                                if (YBA.Common.check.isNull(val)) {
                                    showError(this.obj,"请输入最大备份值");
                                    return false;
                                } else if(YBA.Common.check.isHowLong(val,11)){
                                	showError(this.obj,"最大备份值过大");
                                    return false;
                                } else if(!YBA.Common.check.isNum(val,{"min":1,"decimals":0})){
                                	showError(this.obj,"请输入正整数");
                                    return false;
                                } else {
                                    hideError(this.obj,"请输入最大备份值");
                                    return true;
                                }
                            }
                        },
                        authTime: {
                            obj: form.find("[name='authTime']"),
                            fn: function() {
                                var val = this.obj.val();
                                if (YBA.Common.check.isNull(val)) {
                                    showError(this.obj,"请输入授权时长");
                                    return false;
                                } else if(YBA.Common.check.isHowLong(val,20)){
                                	showError(this.obj,"授权时长值过大");
                                    return false;
                                } else if(!YBA.Common.check.isNum(val,{"min":1,"decimals":0})){
                                	showError(this.obj,"请输入正整数");
                                    return false;
                                } else {
                                    hideError(this.obj,"请输入授权时长");
                                    return true;
                                }
                            }
                        },
                        caIdJoinLabId: {
                            obj: form.find("[name='caIdJoinLabId']"),
                            fn: function() {
                                var val = this.obj.val();
                                if (YBA.Common.check.isNull(val)) {
                                    showError(this.obj,"请输入授权方式");
                                    return false;
                                } else {
                                    hideError(this.obj,"授权的方式的格式为：labId,caId,labType<br/>&emsp;&emsp;(授权方式：0无授权，1key无关联，2授权码，3key关<br/>&emsp;&emsp;联设备，4授权码关联设备)多个授权方式请换行");
                                    return true;
                                }
                            }
                        },
                        username: {
                            obj: form.find("[name='username']"),
                            fn: function() {
                                var val = this.obj.val();
                                if (YBA.Common.check.isNull(val)) {
                                    showError(this.obj,"请输入用户名");
                                    return false;
                                } else if(YBA.Common.check.isHowLong(val,30)){
                                	showError(this.obj,"用户名过长");
                                    return false;
                                } else {
                                    hideError(this.obj,"请输入用户名");
                                    return true;
                                }
                            }
                        },
                        name: {
                            obj: form.find("[name='name']"),
                            fn: function() {
                                var val = this.obj.val();
                                if (YBA.Common.check.isNull(val)) {
                                    showError(this.obj,"请输入真实名字");
                                    return false;
                                } else if(YBA.Common.check.isHowLong(val,10)){
                                	showError(this.obj,"真实姓名过长");
                                    return false;
                                } else {
                                    hideError(this.obj,"请输入真实名字");
                                    return true;
                                }
                            }
                        },
                        mobile: {
                            obj: form.find("[name='mobile']"),
                            fn: function() {
                                var val = this.obj.val();
                                if (YBA.Common.check.isNull(val)) {
                                    showError(this.obj,"请输入手机号");
                                    return false;
                                } if (!YBA.Common.check.isMobile(val)) {
                                    showError(this.obj,"手机号不合法");
                                    return false;
                                } else {
                                    hideError(this.obj,"请输入手机号");
                                    return true;
                                }
                            }
                        },
                        email: {
                            obj: form.find("[name='email']"),
                            fn: function() {
                                var val = this.obj.val();
                                if (YBA.Common.check.isNull(val)) {
                                    showError(this.obj,"请输入邮箱");
                                    return false;
                                } if (!YBA.Common.check.isEmail(val)) {
                                    showError(this.obj,"邮箱不合法");
                                    return false;
                                } else {
                                    hideError(this.obj,"请输入邮箱");
                                    return true;
                                }
                            }
                        }
                    };

                    if (name && dataCheck[name]) {
                        return dataCheck[name].fn();
                    } else {
                        var errLen = 0;
                        for (var i in dataCheck) {
                            if (!dataCheck[i].fn()) {
                                errLen++;
                            }
                        }
                        if (errLen > 0) {
                            return false;
                        }
                        return true;
                    }
                },
                //套餐列表
                getPackage:function(dom){
                    YBA.Data.Admin.getPackage(Biz.vars.searchVar, function(json){
                        if(json.code == 200) {
                            var str = "";
                            for(var i in json.list){
                                str+="<option value='"+json.list[i].id+"'>"+json.list[i].salesPackageName+"</option>";
                            }
                            dom.html(str);
                        }
                    });
                }
            },
            // 初始化时执行的所有事件
            event: {
                //初始化表单验证
                formCheck:function(){
                    var form = Biz.el.jform.find("form");
                    Biz.el.jcheck.each(function() {
                        var t = $(this),
                            input = t.find(".required"),
                            name = input.attr("name"),
                            holder = input.siblings(".message");
                        input.blur(function() {
                            Biz.fn.formCheck(form,name);
                        }).focus(function() {
                            holder.show();
                        });;
                    });
                },
                //初始化套餐
                initPackage:function(){
                	Biz.fn.getPackage(Biz.el.jpackage);
                },
                //新增客户
                addCustomer:function(){
                    Biz.el.jaddBtn.bind('click', function() {
                        var form = Biz.el.jform.find("form");
                        if (!Biz.fn.formCheck(form)) {
                            return false;
                        }else{
                        var form = Biz.el.jform.find("form");
                        var data = form.serialize();
                        YBA.Data.Admin.addCustomer(data, function(json){
                            if(json.code == 200) {
                                YBA.Plugin.msgtip({type: "o", msg: "添加成功"});
                                setTimeout(function(){
                                	location.href = "customerList.jsp";
                                }, 1500);
                            }else{
                                YBA.Plugin.msgtip({type: "i", msg: json.desc});
                            }
                        });
                        }
                    });
                },
                //重置表单
                resetFrom:function(){
                    Biz.el.jresetBtn.bind("click",function(){
                    	$("input").val("");
                    	$("textarea").val("");
                        location.reload();
                    })
                }
            }
        };

        Biz.init();

        return Biz;
    }
});
